#ifndef FORMCFGRULESNIPPETS_H
#define FORMCFGRULESNIPPETS_H

#include <QAction>
#include <QCloseEvent>
#include <QMessageBox>
#include <QPointer>
#include <QSettings>
#include <QSortFilterProxyModel>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QVariant>
#include <QWidget>


#include "install.h"
#include "rulesnippetstableview.h"
#include "formdlgrulesnippet.h"
#include "rulesnippetssqltablemodel.h"
#include "databasemanager.h"




namespace Ui {
class FormCfgRuleSnippets;
}

/**
\file formcfgrulesnippets.h

\class FormCfgRuleSnippets

\brief Browse Rulesnippets and call add amend delete rulesnippet form

\todo Display rulesets and rulesnippets in alaphabetical order of name

\todo If rulesets and rulesnippets are displayed in alphabetical order
      then ensure correct line is selected after adding/amending

***************************/
class FormCfgRuleSnippets : public QWidget
{
    Q_OBJECT

public:

    static const int REC_ADD;
    static const int REC_EDIT;
    static const int REC_DELETE;

    /**
    \brief Default Constructor used when form is used to add/amend/delete rule
           snippets
      ******************/
    explicit FormCfgRuleSnippets(QWidget *parent = 0, Qt::WindowFlags f = 0);

    /**
    \brief Constructor used when form is employed as a seperate window and used to
           insert snippets into rulesets.

     The required behaviour is obtained by a combination of the popSnippetSelect
     parameter and the Qt::WindowFlags parameter. This means that the pop up window
     is hidden when the application is closed or the ruleset form is hidden.

     \param popupSnippetSelect - true if form is to be displayed as pop up seperate window else false
     \param parent - reference to QT parent Object
     \param f - Window behaviour flag
      ***********************/
    explicit FormCfgRuleSnippets(bool popupSnippetSelect, QWidget *parent = 0, Qt::WindowFlags f = 0);
    ~FormCfgRuleSnippets();

    /**
      \brief Get a reference to the rulesnippet view
      \return pointer to view
      *****************/
    RuleSnippetsTableView * getView();

    /**
      \brief Get a reference to the rulesnippet model
      \return pointer to model
      *****************/
    RuleSnippetsSqlTableModel *getModel() { return model; }

    /**
      \brief Get data from column of current record by using the column name
      \param colName - name of column
      \return data contained in colName
      **************/
    virtual QVariant getColumnData(QString colName);

    /**
    \brief Displays message box anouncing snippet cannot be deleted as its in use.

    \param title - Message Box Title
    \param snippet - name of snippet that was to be deleted
    \param rulesets - list of ruleset names in which the snippet occurs

    \return void
      ************************/
    virtual void displayMsgBox(QString title, QString snippet, QStringList rulesets);


    virtual QVariant getUpdatedName();
    virtual void setUpdatedName(QVariant name);
    QVariant newName;

signals:

    /**
      \brief Propogates a rule snippet which is added to a ruleset by the
             FormCfgRuleset class.

      \param useInclude - if true include a link to the snippet
             else paste the contents of the snippet
      \param id - primary key of code snippet row
      \param name - name of snippet
      \param snippets - content of snippet
      ***************************/
    void addSnippet(bool useInclude, int id, QString name, QString snippets);

public slots:

    /**
    \brief When the form is displayed set the focus to the rulesnippet browse
     window and select the first row then calls currentRowChanged().

    \param event generated by QT
    ****************/
    virtual void showEvent(QShowEvent *event);

    /**
     \brief When the current row changes the rulesnippet is displayed in a
     readonly text box.
      *****************/
    virtual void currentRowChanged();

    /**
      \brief Calls routine to add a rulesnippet row
      ******************/
    virtual void slotBtnAdd();

    /**
      \brief Calls routine to edit a rulesnippet row
      ******************/
    virtual void slotBtnEdit();

    /**
      \brief Calls routine to delete a rulesnippet row
      ******************/
    virtual void slotBtnDelete();

    /**
    \brief Emits a signal which asks for the current snippet to be included in a
           ruleset
      ******************/
    virtual void slotIncludeSnippet();

    /**
    \brief Emits a signal which asks for the current snippet to be pasted into a
           ruleset
      ******************/
    virtual void slotPasteSnippet();

    /**
    \brief Close the form
      **********************/
    virtual void slotCloseWindow();

    /**
    \brief Perform the action selected from the pop up context menu
    \param selectedAction - selected menu option
      **************************/
    virtual void slotMenuSelection(QAction * selectedAction);

    /**
      \brief enable - enable or display a pop up context menu
      *********************/
    virtual void enableContextMenu(bool enable);


    virtual void slotRowsAboutToBeInserted(const QModelIndex &index, int start, int end);

protected:

    QPointer<RuleSnippetsSqlTableModel> model;
    QPointer<QSortFilterProxyModel> proxyModel;

    bool popupSnippetSelect;
    QString organization;
    QString application;

    /**
    \brief Constructor called from public constructors to perform initialisation
           common to all instances of class.
      **********************/
    virtual void commonConstructor();

    /**
    \brief Save size and position of window if form initiated as a pop up window
      ********************/
    virtual void saveSettings();

    /**
      \brief Called by QT when form is to be closed
      \param event - QT generated close event
      ****************/
    void closeEvent(QCloseEvent *event);

    /**
      \brief Redundant - not used : displays alert box asking if form is to be closed
      \return QMessageBox button pressed
      ***************/
    virtual int quitYesNo();

private:

    Ui::FormCfgRuleSnippets *ui;
};

#endif // FORMCFGRULESNIPPETS_H
